﻿using ChatGPT_SerialPortDemo.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace ChatGPT_SerialPortDemo.Bussinesses
{
    public class SettingsManager
    {
        private readonly static string configFilePath = "Config.xml";

        private static SettingsManager instance;
        private static readonly object lockObject = new object();
        // 获取 SettingManager 实例的方法
        public static SettingsManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockObject)
                    {
                        if (instance == null)
                        {
                            instance = new SettingsManager();
                        }
                    }
                }
                return instance;
            }
        }

        public SettingsManager()
        {
        }

        public void LoadSettings(MainViewModel viewModel)
        {
            if (File.Exists(configFilePath))
            {
                XDocument doc = XDocument.Load(configFilePath);

                // 读取接收设置
                var receiveSettings = doc.Descendants("ReceiveSettings").FirstOrDefault();
                if (receiveSettings != null)
                {
                    viewModel.SelectedRecvDataMode = (MainViewModel.DataMode)Enum.Parse(typeof(MainViewModel.DataMode),(receiveSettings.Attribute("RecvDataMode").Value));
                    viewModel.IsAutoWrap = bool.Parse(receiveSettings.Attribute("IsAutoWrap").Value);
                    viewModel.IsShowSent = bool.Parse(receiveSettings.Attribute("IsShowSent").Value);
                    viewModel.IsShowTime = bool.Parse(receiveSettings.Attribute("IsShowTime").Value);
                }

                // 读取发送设置
                var sendSettings = doc.Descendants("SendSettings").FirstOrDefault();
                if (sendSettings != null)
                {
                    viewModel.SelectedSendDataMode = (MainViewModel.DataMode)Enum.Parse(typeof(MainViewModel.DataMode), (sendSettings.Attribute("SendDataMode").Value));
                    viewModel.IsSendLineByLine = bool.Parse(sendSettings.Attribute("IsSendLineByLine").Value);
                    viewModel.IsAutoResend = bool.Parse(sendSettings.Attribute("IsAutoResend").Value);
                    viewModel.ResendInterval = int.Parse(sendSettings.Attribute("ResendInterval").Value);
                    viewModel.LineByLineInterval = int.Parse(sendSettings.Attribute("LineByLineInterval").Value);
                }

                // 读取历史发送数据
                var history = doc.Descendants("History").FirstOrDefault();
                if (history != null)
                {
                    foreach (var command in history.Elements("Command"))
                    {
                        viewModel.History.Add(command.Value);
                    }
                }
            }
        }

        public void SaveSettings(MainViewModel viewModel)
        {
            XDocument doc = new XDocument(
                new XElement("Settings",
                    new XElement("ReceiveSettings",
                        new XAttribute("RecvDataMode", viewModel.SelectedRecvDataMode),
                        new XAttribute("IsAutoWrap", viewModel.IsAutoWrap),
                        new XAttribute("IsShowSent", viewModel.IsShowSent),
                        new XAttribute("IsShowTime", viewModel.IsShowTime)),
                    new XElement("SendSettings",
                        new XAttribute("SendDataMode", viewModel.SelectedSendDataMode),
                        new XAttribute("IsSendLineByLine", viewModel.IsSendLineByLine),
                        new XAttribute("IsAutoResend", viewModel.IsAutoResend),
                        new XAttribute("ResendInterval", viewModel.ResendInterval),
                        new XAttribute("LineByLineInterval", viewModel.LineByLineInterval)),
                    new XElement("History",
                        viewModel.History.Select(command => new XElement("Command", command)))
                )
            );

            doc.Save(configFilePath);
        }
    }
}
